﻿<?php
class DB{
	/**
	 * php 操作mysql的函数封装
	 */
	
	var $conn;
	var $select_db;
	
	function DB($host, $user, $passwd, $db){
		$this->conn = mysql_connect($host, $user, $passwd) or die(mysql_error());
		$this->select_db = mysql_select_db($db, $this->conn) or die(mysql_error());
		mysql_query("SET NAMES 'utf8'");
	}

	/**
	 * 关闭数据库链接
	 */
	function closeDB(){
		$result = mysql_close($this->conn) or die(mysql_error());
		return $result;
	}

	/**
	 * 插入数据
	 */
	function insert($sql){
		$result = mysql_query($sql, $this->conn);
		return $result;
	}
	
	/**
	 * 更新数据
	 */
	function update($sql){
		$result = mysql_query($sql, $this->conn);
		return $result;
	}

	/**
	 * 删除数据
	 */
	function del($sql){
		$result = mysql_query($sql, $this->conn);
		return $result;
	}

	/**
	 * 读取单个数据
	 */
	function getOne($sql){
		$result = mysql_query($sql, $this->conn);
		$row = mysql_fetch_row($result);
		return $row[0];
	}

	/**
	 * 读取一行数据
	 */
	function getRow($sql){
		$row = array();
		$result = mysql_query($sql, $this->conn);
		$row = mysql_fetch_assoc($result);
		if($row)
			$row = array_change_key_case($row, CASE_LOWER);
		mysql_free_result($result);
		return $row;
	}

	/**
	 * 读取一批数据
	 */
	function getRows($sql){
		$row = array();
		$rows = array();
		$result = mysql_query($sql, $this->conn);
		while ($row = mysql_fetch_assoc($result)){
			$rows[] = array_change_key_case($row, CASE_LOWER);
		}
		mysql_free_result($result);
		return $rows;
	}
}
?>